package com.springdingyiaop;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 作者: LCG
 * 日期: 2019/9/20 19:13
 * 描述:
 */

@Aspect
@Component
public class MyLogAop {

    @Autowired
    private  RabbitTemplate rabbitTemplate;

    @Pointcut("execution(public * com.springmvc.*.*(..))")
    public void aop(){}


    @Before("aop() && @annotation(com.springdingyiaop.MyLog)")
    public void before(JoinPoint joinPoint){

        MethodSignature signature =(MethodSignature)joinPoint.getSignature();

        Method method = signature.getMethod();

        MyLog annotation = method.getAnnotation(MyLog.class);

        System.out.println("=====info===="+annotation.info());

        if(annotation.isOpen()){
            String info = annotation.info();
            Object[] args = joinPoint.getArgs();

            String jsonString = JSON.toJSONString(args);

            System.out.println("==========="+jsonString);

            rabbitTemplate.convertAndSend("Exchange00000","directMessage",jsonString);

        }

    }

    @After("aop() && @annotation(com.springdingyiaop.MyLog)")
    public void after(JoinPoint joinPoint){
        System.out.println("========after==========");
    }

}
